package com.glsc.ngateway.opmanage.controller.deposit;


import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.deposit.DepositoryDayEndTrial;
import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.deposit.DepositoryDayEndTrialCcb;
import com.glsc.ngateway.opmanage.aspect.UserCache;
import com.glsc.ngateway.opmanage.dto.DepositoryDayEndTrialDto;
import com.glsc.ngateway.opmanage.dto.DepositoryDto;
import com.glsc.ngateway.opmanage.service.deposit.DepositoryDayEndTrialCcbService;
import com.glsc.ngateway.opmanage.service.deposit.DepositoryDayEndTrialService;
import com.glsc.ngateway.opmanage.utils.DateUtils;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 保证金每日试算（新）日终
 */
@RestController
@RequestMapping("/api/depository/dayEndTrial")
@Slf4j
public class DepositoryDayEndTrialController {

    @Resource
    private UserCache userCache;

    @Resource
    private DepositoryDayEndTrialService depositoryDayEndTrialService;

    @Resource
    private DepositoryDayEndTrialCcbService depositoryDayEndTrialCcbService;

    /**
     * 创建今日数据
     *
     * @return
     */
    @GetMapping("/create")
    public PlatformResponse<?> createDate() {
        depositoryDayEndTrialService.createDate();
        return PlatformResponse.success();
    }

    /**
     * 分页查找
     *
     * @param dto
     * @param request
     * @return
     */
    @PostMapping("/page")
    public PlatformResponse<Page<DepositoryDayEndTrial>> pageTemplate(@RequestBody DepositoryDto dto, HttpServletRequest request) {

        Map<String, Object> param = new HashMap<>();
        if (StringUtils.isNotBlank(dto.getEffectDate())) {
            param.put("LIKE_effectDate", dto.getEffectDate());
        }
        Page<DepositoryDayEndTrial> page = depositoryDayEndTrialService.page(dto.getPageNo(), dto.getPageSize(), param);

        return PlatformResponse.successData(page);
    }

    @PostMapping("/saveCcbAmount")
    public PlatformResponse<DepositoryDayEndTrialCcb> saveCcbAmount(@RequestBody DepositoryDayEndTrialDto dto, HttpServletRequest request) {
        if (Objects.nonNull(dto.getCcb3568Amount()) && StringUtils.isNotBlank(dto.getEffectDate())) {
            String today = DateUtils.getNowDateString();
            if (!today.equals(dto.getEffectDate())) {
                return PlatformResponse.failedMsg("只能修改今日3568数据");
            }
            DepositoryDayEndTrialCcb ccb = depositoryDayEndTrialCcbService.save(DepositoryDayEndTrialCcb.builder().bankName("建行3568").effectDate(today).accountBalance(dto.getCcb3568Amount()).build());
            return PlatformResponse.successData(ccb);
        }else {
            return PlatformResponse.failedMsg("数据异常，不可修改");
        }
    }


    /**
     * 保证金试算如初汇总数据
     *
     * @param dto
     * @param request
     * @return
     */
    @PostMapping("/sumAmount")
    public PlatformResponse<DepositoryDayEndTrialDto> sumAmount(@RequestBody DepositoryDto dto, HttpServletRequest request) {
        DepositoryDayEndTrialDto res = DepositoryDayEndTrialDto.builder().accountBalance(BigDecimal.ZERO).bankSecTranBalance(BigDecimal.ZERO)
                .preCounterBalance(BigDecimal.ZERO).currentCounterBalance(BigDecimal.ZERO).adjustBalance(BigDecimal.ZERO)
                .adjustValue(BigDecimal.ZERO).interest(BigDecimal.ZERO).build();

        Map<String, Object> param = new HashMap<>();
        if (StringUtils.isNotBlank(dto.getEffectDate())) {
            param.put("LIKE_effectDate", dto.getEffectDate());
        }
        Page<DepositoryDayEndTrial> page = depositoryDayEndTrialService.page(1, 1000, param);
        if (Objects.nonNull(page) && CollectionUtils.isNotEmpty(page.getContent())) {
            for (DepositoryDayEndTrial trial : page.getContent()) {
                if (Objects.nonNull(trial.getInterest())) {
                    res.setInterest(res.getInterest().add(trial.getInterest()));
                }
                if (Objects.nonNull(trial.getAdjustValue())) {
                    res.setAdjustValue(res.getAdjustValue().add(trial.getAdjustValue()));
                }
                if (Objects.nonNull(trial.getAdjustBalance())) {
                    res.setAdjustBalance(res.getAdjustBalance().add(trial.getAdjustBalance()));
                }
                if (Objects.nonNull(trial.getAccountBalance())) {
                    res.setAccountBalance(res.getAccountBalance().add(trial.getAccountBalance()));
                }
                if (Objects.nonNull(trial.getCurrentCounterBalance())) {
                    res.setCurrentCounterBalance(res.getCurrentCounterBalance().add(trial.getCurrentCounterBalance()));
                }
                if (Objects.nonNull(trial.getBankSecTranBalance())) {
                    res.setBankSecTranBalance(res.getBankSecTranBalance().add(trial.getBankSecTranBalance()));
                }
                if (Objects.nonNull(trial.getPreCounterBalance())) {
                    res.setPreCounterBalance(res.getPreCounterBalance().add(trial.getPreCounterBalance()));
                }
                DepositoryDayEndTrialCcb ccb = depositoryDayEndTrialCcbService.findByNameAndDate("建行3568", DateUtils.getNowDateString());
                if (Objects.isNull(ccb) || Objects.isNull(ccb.getAccountBalance())) {
                    res.setCcb3568Amount(BigDecimal.ZERO);
                }else {
                    res.setCcb3568Amount(ccb.getAccountBalance());
                }

            }
        }

        return PlatformResponse.successData(res);
    }

    /**
     * 根据id查找
     *
     * @param
     * @return
     */
    @GetMapping("/findById")
    public PlatformResponse<DepositoryDayEndTrial> findTemplateById(@ApiParam(required = false, value = "id") @RequestParam(required = true) Integer id) {
        return PlatformResponse.successData(depositoryDayEndTrialService.findById(id));
    }

    /**
     * 保存
     *
     * @param obj
     * @return
     */
    @PostMapping("/save")
    public PlatformResponse<?> save(@RequestBody DepositoryDayEndTrial obj) {
        depositoryDayEndTrialService.save(obj);
        return PlatformResponse.success();
    }

    /**
     * 根据id删除
     *
     * @param obj
     * @return
     */
    @PostMapping("/delete")
    public PlatformResponse<?> deleteTemplate(@RequestBody DepositoryDayEndTrial obj) {
        depositoryDayEndTrialService.deleteById(obj.getId());
        return PlatformResponse.success();
    }

    /**
     * 批量删除
     *
     * @param obj
     * @return
     */
    @PostMapping("/deleteByIds")
    public PlatformResponse<?> deleteTemplate(@RequestBody DepositoryDto obj) {

        if (Objects.isNull(obj)) {
            return PlatformResponse.failedMsg("删除数据不可为空");
        }
        depositoryDayEndTrialService.deleteByIds(obj.getIds());
        return PlatformResponse.success();
    }


}
